package com.sc.nft.dao;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.Push;
import com.sc.nft.mapper.PushMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * @author 刘国航
 * @desc
 * @since 2024/6/14 16:06
 */
@Slf4j
@Repository
public class PushDao extends ServiceImpl<PushMapper, Push> {

    public Page<Push> pageByTitle(Page<Push> page, String title) {
        LambdaQueryWrapper<Push> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StrUtil.isNotBlank(title), Push::getPushTitle, title);
        queryWrapper.orderByDesc(Push::getId);
        return baseMapper.selectPage(page, queryWrapper);
    }

    public List<Push> getByTitle(String title) {
        LambdaQueryWrapper<Push> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Push::getPushTitle, title);
        return baseMapper.selectList(queryWrapper);
    }

    public List<Push> getUnPushedScheduleListFiveMinuteLater() {
        LambdaQueryWrapper<Push> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Push::getEnableSchedulePush, true);
        queryWrapper.eq(Push::getIsPushed, false);
        queryWrapper.eq(Push::getStatus, true);
        queryWrapper.le(Push::getSchedulePushTime, DateUtil.date().offsetNew(DateField.MINUTE, 5));
        return baseMapper.selectList(queryWrapper);
    }

}
